package com.atguigu.stack;

import org.junit.Test;

import java.util.Stack;

public class StackArrayTest {

    @Test
    public void test1(){
        StackArray<Integer>stackArray=new StackArray<>(5);
        for (int i = 0; i < 5; i++) {
            stackArray.push(i);
        }
        System.out.println(stackArray);
        Integer integer = stackArray.pop();
        System.out.println(integer);//4
        for (int i = 0; i < 10; i++) {
            stackArray.push(i+10);
        }
        System.out.println(stackArray);
    }
    @Test
    public void test2(){
        String s="(((())))";
        boolean blacket = isBlacket(s);
        System.out.println(blacket);
    }
    public boolean isBlacket(String s){
        char[] chars = s.toCharArray();
        int i=0,top=-1;
        while(i<chars.length){
            if(chars[i]=='('){
                top++;
            }else if(chars[i]==')'){
                if(top==-1){
                    break;
                }else{
                    top--;
                }
            }
            i++;
        }
        return i==chars.length&&top==-1;
    }
    @Test
    public void test3(){


    }
    public boolean isValid(String s) {
        //括号匹配
        Stack<Character> stack = new Stack<>();
        char[] charArray = s.toCharArray();
        int i=0;
        while (i<charArray.length){
            if(charArray[i]=='('||charArray[i]=='['||charArray[i]=='{'){
                stack.push(charArray[i]);
            }else if(charArray[i]==')'||charArray[i]==']'||charArray[i]=='}'){
                if(!stack.isEmpty()){
                    Character character = stack.pop();
                    if(character!=charArray[i]){
                        return false;
                    }
                }else{
                    break;
                }
            }
            i++;
        }
        return i==charArray.length&&stack.isEmpty();
    }
    public Character toChangeCharacter(Character character){
        if(character==')'){
            return '(';
        }else if(character==']'){
            return '[';
        }else{
            return '{';
        }
    }
    public boolean isValid2(String s){
        Stack<Character>stack=new Stack<>();
        for(char c:s.toCharArray()){
            if(c=='('){
                stack.push(')');
            }else if(c=='{'){
                stack.push('}');
            }else if(c=='['){
                stack.push(']');
            }else if(stack.isEmpty()||stack.pop()!=c){
                return false;
            }
        }
        return stack.isEmpty();
    }
    public boolean isVaild3(String s){
        Stack<Character>stack=new Stack<>();
        for(int i = 0 ; i < s.length() ; i++){
            char c = s.charAt(i);
            if(c=='['||c=='{'||c=='('){
                stack.push(c);
            }else{
                if(stack.isEmpty()){
                    return false;
                }
                Character topChar = stack.pop();
                if(c==')'&&topChar!='('){
                    return false;
                }
                if(c=='}'&&topChar!='{'){
                    return false;
                }
                if(c==']'&&topChar!=']'){
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
}
